package com.netsar.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.netsar.modelo.Reservas;

public class ReservasDAO extends DAO{

	private static ReservasDAO hotelDAO;  

	public static final ReservasDAO getInstance() {  
		if (hotelDAO == null) {  
			hotelDAO = new ReservasDAO();  
		}  
		return hotelDAO;  
	}  	



	public void salva(Reservas r){
		//obtendo o EntityManager
		EntityManager em = getEntityManager();
		try{
			if (!em.getTransaction().isActive()) 
				//inicia o processo de transacao
				em.getTransaction().begin();
			//faz a persistencia
			em.persist(r);
			//faz comit no banco
			em.getTransaction().commit();
		}catch (Exception e) {
			//cancela a transacao em caso de erro
			em.getTransaction().rollback();
			e.printStackTrace();
		}finally{
			em.close();
		}
	}



	public void remove(Reservas r){
		//obtendo o Eem.getReference(Reservas.class, r.getIdReserva())ntityManager
		EntityManager em = getEntityManager();
		try{
			if (!em.getTransaction().isActive()) 
				//inicia o processo de transacao
				em.getTransaction().begin();
			//faz a persistencia
			em.remove(em.getReference(Reservas.class, r.getCodigoHotel()));
			//faz comit no banco
			em.getTransaction().commit();
		}catch (Exception e) {
			//cancela a transacao em caso de erro
			e.printStackTrace();
			em.getTransaction().rollback();
		}finally{
			em.close();
		}
	}



	public List<Reservas> recuperaReservaHoteis(){

		EntityManager em = getEntityManager();
		try{
			Query q = em.createQuery("from Reservas obj order by obj.codigoHotel asc");
			return q.getResultList();
		}finally{
			em.close();
		}

	}


}
